var BC_WS = {
	init: function(url) {
		if("WebSocket" in window) {
			if(typeof url == "undefined") {
				url = BCConfig.wsUrl
			}
			BC_WS.url = url;
			console.log("ws 链接：" + url);
			var ws = new WebSocket(url);
			ws.onopen = function() {
				console.log("BC_WS onopen");
				BC_WS.onopen();
			};
			ws.onmessage = function(evt) {
				console.log(evt);
				BC_WS.onmessage(evt.data);
			};
			ws.onclose = function(ent) {
				console.log("BC_WS onclose");
				console.log(ent);
				BC_WS.onclose();
			};
			ws.onerror = function(env) {
				console.log("error");
				console.log(env);
			},
			setInterval(BC_WS.hearts,1000 * 50)
			BC_WS.ws = ws;
		} else {
			alert("您的浏览器不支持 WebSocket!");
		}
	},
	hearts:function(){
		
	},
	onopen: function() {
	},
	onmessage: function(data) {
		console.log("ws 接收:" + data);
		var obj = JSON.parse(data);
		BC_WS.notifyFuncs(obj, obj.cmd);
	},
	onclose: function() {
		BC_Notify.show({
			msg:"链接已经关闭，请刷新页面重新链接",
			actions:{
				title:"刷新",
				func:function(){
					window.history.go(0);
				}
			}
		});
	},
	send: function(data) {
		if(BC_WS.ws.readyState == 1) {
			var str = JSON.stringify(data);
			console.log("ws 发送:" + str);
			BC_WS.ws.send(str);
		} else {
			var obj = {
				code: 500,
				result:2,
				msg: "ws.readyState：" + BC_WS.ws.readyState > 1 ? "链接关闭" : "链接尚未建立"
			};
			BC_WS.notifyFuncs(obj);
		}
	},
	onTypeMsgFuncs: [],
	addObserver: function(type, observerFunc, always) {
		var p = {
			cmd: type,
			func: function(data) {
				 var stillObserver = observerFunc(data, p);
				 if(typeof stillObserver =="boolean" ){
				 	if(stillObserver == true){
				 		return;
				 	}else{
				 		BC_WS.onTypeMsgFuncs.pop(p);
				 	}
				 }
				if(typeof always == "boolean" && always == true) {} else {
					BC_WS.onTypeMsgFuncs.pop(p);
				}
			}
		};
		BC_WS.onTypeMsgFuncs.push(p);
	},
	talk1: function(param) {
		var p = new Promise(function(resolve, reject) {
			BC_WS.addObserver("talk2", function(data) {
				if(data.result == 1) {
					resolve(data);
				} else {
					reject(data);
				}
			});
			BC_WS.send(param);
		});
		return p;
	},
	notifyFuncs: function(obj, cmd) {
		var q = [];
		for(var i = 0; i < BC_WS.onTypeMsgFuncs.length; i++) {
			var p = BC_WS.onTypeMsgFuncs[i];
			q.push(p);
		}
		for(var i = 0; i < q.length; i++) {
			var p = q[i];
			if(typeof cmd != "undefined") {
				var cmds = cmd.split(",");
				for(var j in cmds) {
					if(cmds[j] == p.cmd) {
						p.func(obj);
					}
				}
			} else {
				p.func(obj);
			}
		}
		for(var i = 0; i < q.length; i++) {
			var p = q[i];
			q.pop(p);
		}
		q = null;
	}
}
//#### 根据type接受数据
BC_POST = {
	search: function(param) {
		var p = new Promise(function(resolve, reject) {
			$.ajax({
				url: BCConfig.jsonUrl + "/query",
				data: param,
				success: function(data) {
					resolve(data);
				},
				error: function(err) {
					reject({
						code: 100,
						msg: err,
						data: err
					});
				},
			});
		});
		return p;
	},
	canDo: function(param) {
		var p = new Promise(function(resolve, reject) {
			$.ajax({
				url: BCConfig.jsonUrl + "/canDo",
				data: param,
				method: "post",
				dataType: "json",
				success: function(data) {
					console.log(data);
					resolve(data);
				},
				error: function(err) {
					reject({
						code: 100,
						msg: err.message,
						data: err
					});
				},
			});
		});
		return p;
	}
}